<%-- 
    Document   : funciones
    Created on : 16/03/2012, 06:41:03 PM
    Author     : Angel
--%>
<%@page import="java.security.MessageDigest"%>
<%@page import="java.sql.ResultSet"%>
<%@page language="Java" import="java.sql.*" %>
<jsp:useBean id="db" scope="request" class="conexion.DB" />
<jsp:useBean id="mail" scope="session" class="fantasy.SendMail" />
<%
    //campo_nombre,campo_apellido,campo_correo,campo_direccion,campo_sexo,campo_pais,fecha_nacimiento,campo_contra
    String variable = "null";
    int sol = 0;
    int sol2 = 0;
    boolean conectado = false;
    System.out.println("entro con login = " + request.getParameter("login_usuario"));
    if(request.getParameter("campo_nombre") != null){
        if(db.connect()){
           sol =  db.insertar("email_user,name_user,last_name_user,borndate_user,sex_user,id_city_user,id_role_user,real_money_user", "'"+request.getParameter("campo_correo")+"','"+request.getParameter("campo_nombre")+"','"+request.getParameter("campo_apellido")+"','"+request.getParameter("fecha_nacimiento")+"','"+request.getParameter("campo_sexo")+"','1','2',"+request.getParameter("campo_tipo_dinero"), "user");
           if(sol != 0){
               MessageDigest alg = MessageDigest.getInstance("MD5");
               alg.reset(); 
               alg.update(request.getParameter("campo_contra").getBytes());
               byte[] digest = alg.digest();
            StringBuffer hashedpasswd = new StringBuffer();
            String hx;
            for (int i=0;i<digest.length;i++){
                    hx =  Integer.toHexString(0xFF & digest[i]);
                    //0x03 is equal to 0x3, but we need 0x03 for our md5sum
                    if(hx.length() == 1){hx = "0" + hx;}
                    hashedpasswd.append(hx);
            }
            String password = hashedpasswd.toString();
               sol2 = db.insertar("email_user_login,password_login,status_login,connected_login", "'"+request.getParameter("campo_correo")+"','"+password+"','0',"+conectado, "login");
              if(sol2 != 0){
                  variable ="1";
                  String correo = request.getParameter("campo_correo");
                  System.out.println("se va a enviar correo a "+request.getParameter("campo_correo"));
                  try{mail.postMail(correo);}catch(Exception e){e.printStackTrace();}
              }else{
                  variable ="null";
              }
           }else{
               variable ="null";
           }
        }else{
            variable ="null";
        }
        
    }else{
        variable ="null";
    }   
    
    out.print(variable);
    
%>